package com.sibbay.syncbox.adapter.box;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.box.androidlib.activities.BoxAuthentication;

public class AuthenticationActivity extends Activity {

	// NOTE! special case!
	static interface OnLoginDone {
		void done(final String token);
	}; 
	static OnLoginDone sOnLoginDone;  
	// 
	
    private static final int AUTH_REQUEST_CODE = 100;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // For convenience, the Box Android library includes an activity which
        // you can launch to handle authentication. This saves you from having
        // to implement the login webviews yourself.
        // If you would prefer to create the login webviews yourself, see the
        // source code in com.box.androidlib.activities.BoxAuthentication for
        // guidance, as well as the API docs for authentication:
        // http://developers.box.net/w/page/12923915/ApiAuthentication
        Intent intent = new Intent(this, BoxAuthentication.class);
        intent.putExtra("API_KEY", BoxSyncAdapterSynchronous.CONST_API_KEY); // API_KEY is required
        startActivityForResult(intent, AUTH_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == AUTH_REQUEST_CODE) {
            if (resultCode == BoxAuthentication.AUTH_RESULT_SUCCESS) {
            	sOnLoginDone.done(data.getStringExtra("AUTH_TOKEN"));
            } else if (resultCode == BoxAuthentication.AUTH_RESULT_FAIL) {
            	sOnLoginDone.done(null);
            }
            finish();
        }
    }
}
